package main

import (
	"flag"
	"fmt"
	"gitee.com/dn-jinmin/scenario/06.restart/1.5.5/example/internal/config"
	"gitee.com/dn-jinmin/scenario/06.restart/1.5.5/example/internal/handler"
	"gitee.com/dn-jinmin/scenario/06.restart/1.5.5/example/internal/svc"
	"github.com/zeromicro/go-zero/core/proc"
	"sync"
	"time"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

var configFile = flag.String("f", "etc/example-api.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)
	var wg sync.WaitGroup
	for {

		wg.Add(1)
		go func() {
			defer wg.Done()
			fmt.Println("初次启动")
			Run(c)
		}()
		go func() {
			time.Sleep(2 * time.Second)
			fmt.Println("重启")
			proc.WrapUp()
			//proc.Shutdown()
		}()
		wg.Wait()
	}
}

func Run(c config.Config) {
	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()

	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
